package com.blb.service.impl;

import com.blb.entity.Brand;
import com.blb.entity.BrandType;
import com.blb.entity.Comtype;
import com.blb.mapper.BrandMapper;
import com.blb.mapper.ComtypeMapper;
import com.blb.service.IBrandService;
import com.blb.service.IBrandTypeService;
import com.blb.utils.NowUserUtils;
import com.blb.utils.UUIDUtils;
import com.mysql.jdbc.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author txy
 * @since 2019-07-10
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {

    @Autowired
    private ComtypeMapper comtypeMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private IBrandTypeService brandTypeService;

    /**
     * 获取分类
     */
    @Override
    public List<Comtype> productTypeNameList() {
        List<Comtype> comtypeslist = comtypeMapper.selectByExample(null);
        return comtypeslist;
    }

    /**
     * 添加商品品牌
     * 添加商品品牌与商品商品类别
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Brand brand, String comtypeName) throws Exception {

        //添加品牌对象
        brand.setId(UUIDUtils.getID());
        brand.setCreateTime(new Date());
        brand.setCreateUser(NowUserUtils.getNowUser().getUsername());
        brandMapper.insert(brand);

        //添加商品品牌 与 商品类型 关联表
        if (!StringUtils.isNullOrEmpty(comtypeName)) {
            String[] comtypeNames = comtypeName.split(",");
            for (String combrand : comtypeNames) {
                BrandType brandType = new BrandType();
                brandType.setBrandId(brand.getId());
                brandType.setTypeId(combrand);
                brandTypeService.save(brandType);
            }
        }

    }

    /**
     * 修改商品品牌
     * 修改商品品牌与商品商品类别
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Brand brand, String comtypeName) throws Exception {
        //修改商品品牌
        brand.setUpdateTime(new Date());
        brand.setUpdateUser(NowUserUtils.getNowUser().getUsername());
        brandMapper.update(brand, new QueryWrapper<Brand>().eq("id", brand.getId()));

        //修改商品品牌与类别关联表中的数据
        if (!StringUtils.isNullOrEmpty(comtypeName)) {
            String[] comtypeNames = comtypeName.split(",");
            //1、先删除
            for (String typeid : comtypeNames) {
                brandTypeService.remove(new QueryWrapper<BrandType>().eq("brand_id", brand.getId()));
            }
            //2、在添加
            for (String typeid : comtypeNames) {

                BrandType brandType = new BrandType();

                brandType.setBrandId(brand.getId());
                brandType.setTypeId(typeid);

                brandTypeService.save(brandType);
            }
        }
    }

    /**
     * 删除商品品牌
     * 删除商品品牌与商品商品类别
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<String> ids) throws Exception {
        for (String id : ids) {
            //删除商品品牌表
            brandMapper.deleteById(id);

            //删除商品品牌与类别表
            brandTypeService.remove(new QueryWrapper<BrandType>().eq("brand_id", id));

        }
    }


    /**
     * 根据商品类别id获取品牌信息
     * @param ids
     * @return
     */
    @Override
    public List<Brand> getBrandByComtypeId(String ids) {
        List<Brand> brandByComtypeIds = brandMapper.getBrandByComtypeIds(ids);
        return brandByComtypeIds;
    }

    /**
     * 获取所有品牌
     */
    @Override
    public List<Brand> getBrand() {
        List<Brand> list = brandMapper.selectList(null);
        return list;
    }

}
